﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;   //输入输出。
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Data.SqlClient;

namespace 共享单车管理系统
{
    public partial class guzhuang : Form
    {
        public guzhuang()
        {
            InitializeComponent();
        }

        SqlDataAdapter daProduct, daLog;
        DataSet ds = new DataSet();
        public static string path_source = "";


        void init()//初始化
        {
            DB.GetCn();
            string str = "select * from Bike";
            string sdr = "select * from log";
            daProduct = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(sdr, DB.cn);
            daProduct.Fill(ds, "product_info");
            daLog.Fill(ds, "log_info");
        }
        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dgvpic.DataSource = dvProduct;

        }






        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void combtyep_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtid_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtdizhi_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgvpic_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtid.Text = dgvpic.CurrentRow.Cells["BikeID"].Value.ToString();
            combtyep.Text = dgvpic.CurrentRow.Cells["BikeType"].Value.ToString();
            combzhuangtai.Text = dgvpic.CurrentRow.Cells["Status"].Value.ToString();
            txtmiaoshu.Text = dgvpic.CurrentRow.Cells["miaoshu"].Value.ToString();
            txtdizhi.Text = dgvpic.CurrentRow.Cells["Location"].Value.ToString();
            try
            {
                picyup.Image = Image.FromFile(Application.StartupPath
                + dgvpic.CurrentRow.Cells["img_dizhi"].Value.ToString());
                picyup.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            catch
            {
                picyup.Image = Image.FromFile(Application.StartupPath + "\\" + "无.gif");

            }
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
              if(txtid.Text=="")
            {
                MessageBox.Show("不能为空");
            }

            else
            {
                DialogResult dr = MessageBox.Show("确认上报嘛？","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                if(dr==DialogResult.OK)
                {
                    string filename;
                    string fileFolder;
                    string dateTime = "";
                    filename = Path.GetFileName(path_source);
                    dateTime += DateTime.Now.Year.ToString();
                    dateTime += DateTime.Now.Month.ToString();
                    dateTime += DateTime.Now.Day.ToString();
                    dateTime += DateTime.Now.Hour.ToString();
                    dateTime += DateTime.Now.Minute.ToString();
                    dateTime += DateTime.Now.Second.ToString();
                    filename = dateTime + filename;
                    fileFolder = Directory.GetCurrentDirectory() + "\\" + "Prod_imges\\"
                        + combtyep.SelectedText + "\\";

                    fileFolder += filename;
                    int a=dgvpic.CurrentCell.RowIndex;
                    string str = dgvpic.Rows[a].Cells["BikeID"].Value.ToString();
                    DataRow[] ProRows = ds.Tables["product_info"].Select("BikeID='" + str + "'");
                    ProRows[0]["BikeType"] = combtyep.Text.Trim();
                    ProRows[0]["Status"] =combzhuangtai.Text.Trim();
                   
                    ProRows[0]["miaoshu"] =txtmiaoshu.Text.Trim();
                    ProRows[0]["Location"] = txtdizhi.Text.Trim();


                 

                    if (path_source != "")
                    {
                        File.Copy(path_source, fileFolder, true);


                        ProRows[0]["img_dizhi"] = "\\Prod_imges\\" + combtyep.SelectedText
                            + "\\" + filename;
                    }
                    
                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["username"] = Login.UserID;
                    drLog["Type"] = "修改";
                    drLog["Time"] = DateTime.Now;
                    drLog["table"] = "product表";
                    ds.Tables["log_info"].Rows.Add(drLog);
                    try
                    {
                        SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProduct);
                        daProduct.Update(ds, "product_info");
                        SqlCommandBuilder dbLog = new SqlCommandBuilder(daLog);
                        daLog.Update(ds, "log_info");
                        MessageBox.Show("上报成功");
                        path_source = "";
                        picyup.SizeMode = PictureBoxSizeMode.StretchImage;
                    }

                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    DB.cn.Close();
                }
            }
        }

        private void buttontup_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path_source = ofd.FileName;

                picyup.Image = Image.FromFile(path_source);
                picyup.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void guzhuang_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“carDataSet18.BikeStatus”中。您可以根据需要移动或移除它。
            this.bikeStatusTableAdapter.Fill(this.carDataSet18.BikeStatus);

            init();
            showAll();
        }
    }
}
